package leetcode;

public class _0092 {
    /**
     * 反转链表 II
     */
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode reverseBetween(ListNode head, int left, int right) {
            ListNode dummpy = new ListNode(0,head);
            ListNode p0 = dummpy;
            for(int i=0;i<left-1;i++){
                p0= p0.next;
            }

            ListNode pre  = null;
            ListNode cur = p0.next;
            for(int i=0;i<right-left+1;i++){
                ListNode nxt = cur.next;
                cur.next = pre;
                pre = cur;
                cur = nxt;
            }
            p0.next.next = cur;
            p0.next = pre;
            return dummpy.next;
        }
        private class ListNode {
            ListNode next;
            int val;
            ListNode(){}
            ListNode(int val){
                this.val=val;
            }
            ListNode(int val, ListNode next){
                this.val = val;
                this.next = next;
            }
        }
    }
}
